var shopId = '';
var goodsId = '';
var weekTime = '';
var weekTimeStr = '';
var time = '';
var jobPeopleArr = [];
var jobPeopleId  = '';
var idArr = [];
var count = 1;
var pageNum = 1;
var pageSize = 10;
var status = 0;
var evaluationArr = [];
var evaluationPush = false;
var roomServiceFlag  = '';  // 是否直接上门服务
 
$(document).ready(function(){   
	shopId = '0600400007120190215';
	goodsId = '1';
	getGoodsDetail();
	getJobPeople();
	getShopDetail();
	getDetailEvaluation();
	getUserEvaluation();
});
 // 减少数量
 $('.red').click(function(){
	if(count == 1){
		count = 1;
	}else{
		count --;
	}
	$('.amount').text(count);
 });
 
 // 增加数量
 $('.add').click(function(){
	count ++;
	$('.amount').text(count);
 });

 // 切换图片
 $('#swiper').on('click','.banner',function(){
	$('.top1').attr('src',$(this).attr('src'));
 });

// 选择服务周时间日期
$('.shop-con.fl').on('click','.sldate',function(){
	time = '';
	$('.time').removeClass('select');
	weekTime = $(this).data('date-long');
	weekTimeStr = $('.date').text();
	$(this).addClass('select').siblings().removeClass('select');
	getServiceTime();
	getJobPeople();
});

// 选择服务具体时间日期
$('.shop-con.fl').on('click','.hour',function(){
	time = $(this).text();
	$(this).addClass('select').siblings().removeClass('select');
	getJobPeople();
});

// 请选择服务人员
$(".slstaff").click(function() {
	$(".staff-box").toggle();
})

// 选择服务人员
$('#staff-box').on('click','.staff-list',function(){
	if($(this).hasClass('staff-list-border')){
		$(this).removeClass('staff-list-border');
		var index = idArr.indexOf($(this).data('people-id'));
		console.log(index);
		if(index != -1){
			idArr.splice(index,1);
			console.log(idArr);
		}
	}else{
		if(idArr.length < count){
			$(this).addClass('staff-list-border');
			idArr.push($(this).data('people-id'));
			console.log(idArr);
		}
	}
});

// 取消选择服务人员
$('#catch').on('click','.close',function(){
	var removeId = $(this).parent().data('remove-id');
	for(var j = 0 ; j < jobPeopleArr.length ; j ++){
		if(removeId == jobPeopleArr[j].id){
			console.log(removeId)
			jobPeopleArr.splice(jobPeopleArr[j],1);
		}
	};
	JobPeople();
});

// 选择服务人员确定
$('#staff-box').on('click','.confirm',function(){
	JobPeople();
	$(".staff-box").hide();
});
  
// 退款规则说明
$('#promise').on('mouseover','.illu',function(){
	$(".illu-box").css("display", "block");
});
$('#promise').on('mouseout','.illu',function(){
	$(".illu-box").css("display", "none");
});  

// 立即购买
$('.buynow').click(function(){
	if(time){
		jobPeopleId = idArr.join();
		if(roomServiceFlag == 1){
<<<<<<< HEAD
			window.location.href = 'doorServe.html?dateLong='+weekTime+'&time='+time+'&peopleId='+jobPeopleId+'&shopId='+shopId+'&goodsId='+goodsId+'&count='+count;
		}else{
			window.location.href = 'NodoorServe.html?dateLong='+weekTime+'&time='+time+'&peopleId='+jobPeopleId+'&shopId='+shopId+'&goodsId='+goodsId+'&count='+count;
=======
			window.location.href = 'doorServe.html?dateLong='+weekTimeStr+'&time='+time+'&peopleId='+jobPeopleId+'&shopId='+shopId+'&goodsId='+goodsId;
		}else{
			window.location.href = 'NodoorServe.html?dateLong='+weekTimeStr+'&time='+time+'&peopleId='+jobPeopleId+'&shopId='+shopId+'&goodsId='+goodsId;
>>>>>>> 5632b858270b0bdbe654ff58acee5b133dd38674
		}
	}else{
		alert('请选择服务时间');
	}
});

// 关注店铺
$('#shop-right').on('click','.focus-shop',function(){
	console.log('关注我');
	focusShop();
});

// 查看更多评价
$('.viewmore').click(function(){
	pageNum += 1;
	evaluationPush = true;
	getUserEvaluation();
});

// 切换评价
$('#rate').on('click','.eva-list',function(){
	if(status != $(this).data('status')){
		$(this).addClass('active').siblings().removeClass('active');
		status = $(this).data('status');
		pageNum = 1;
		evaluationPush = false;
		$('.viewmore').text('查看更多评价');
		$('.viewmore').append('<img src="img/viewmore.png" alt="">');
		getUserEvaluation();
	}
});

// 关注/取消关注店铺
function focusShop(){
	$.ajax({
    url: baseUrl+"/hsent-xxyl-user/service/addAttentionShop",
    type:"get",	
    data:"shopId="+shopId,	
    dataType:"json", 
    success:function(res){
      console.log(res);
      if(res.status === 1){
				if($('#focus-heard').hasClass('fa-heart-o')){
					$('#focus-heard').removeClass('fa-heart-o').addClass('fa-heart');
				}else{
					$('#focus-heard').removeClass('fa-heart').addClass('fa-heart-o');
				}
      }
    },
    error:function(res){
          
    },
  })
}

// 用户选择工作人员展示
function JobPeople(){
	var tempArr = [];
	for(var i = 0 ; i < idArr.length ; i ++){
		for(var j = 0 ; j < jobPeopleArr.length ; j ++){
			if(idArr[i] == jobPeopleArr[j].id){
				tempArr.push(jobPeopleArr[j]);
			}
		}
	}
	var data={
		data:tempArr,
		picUrl:picUrl
	};
	console.log(data)
	var html = template('catch-script', data);
	document.getElementById('catch').innerHTML = html;
};

// 获取服务详情
function getGoodsDetail(){
	$.ajax({
    url: baseUrl+"/hsent-xxyl-user/service/tGoodsUserBasicInfo",
    type:"get",	
    data:"shopId="+shopId+'&id='+goodsId,	
    dataType:"json", 
    success:function(res){
      console.log(res);
      if(res.status === 1){
				roomServiceFlag = res.data.roomServiceFlag;
				var tempData = res.data;
				weekTime = tempData.choiceDayVoList[0].dateLong;
				weekTimeStr = tempData.choiceDayVoList[0].dateName;
				tempData.discountMoney = tempData.hscouponUseRule.split(':')[0];
				tempData.discountNum = tempData.hscouponUseRule.split(':')[1];
				if(tempData.aheadNote2 != null){
					tempData.aheadNoteArr = tempData.aheadNote2.split(',');
				}else{
					tempData.aheadNoteArr = [];
				}
        var data={
					data:tempData,
					picUrl:picUrl
				};
        var html = template('goods-detail-script', data);
				document.getElementById('goods-detail').innerHTML = html;
				var html2 = template('promise-script', data);
				document.getElementById('promise').innerHTML = html2;
				var html3 = template('swiper-script', data);
				document.getElementById('swiper').innerHTML = html3;
				var html5 = template('notice-box-script', data);
				var html4 = template('service-box-script', data);
				document.getElementById('service-box').innerHTML = html4;
				document.getElementById('notice-box').innerHTML = html5;
				var html6 = template('pic-box-script', data);
				document.getElementById('pic-box').innerHTML = html6;
				getServiceTime();
      }
    },
    error:function(res){
          
    },
  })
};

// 获取店铺详情
function getShopDetail(){
	$.ajax({
    url: baseUrl+"/hsent-xxyl-user/store/businessInformation",
    type:"get",	
    data:"shopId="+shopId,	
    dataType:"json", 
    success:function(res){
      console.log(res);
      if(res.status === 1){
				var tempData = res.data;
				tempData.entResno = tempData.entResno.substring(0,2)+' '+
        tempData.entResno.substring(2,5)+' '+tempData.entResno.substring(5,7)+' '+
				tempData.entResno.substring(7,11);
				tempData.serviceRangeTime = tempData.serviceRangeTime.split(',')[0]+'至'+tempData.serviceRangeTime.split(',')[1];
        var data={
          data:tempData,
				};
				var html = template('shop-right-script', data);
				document.getElementById('shop-right').innerHTML = html;
      }
    },
    error:function(res){
          
    },
  })
}

// 获取服务具体时间
function getServiceTime(){
	$.ajax({
    url: baseUrl+"/hsent-xxyl-user/purchaseNotice/getChoiceTime",
    type:"get",	
    data:"choiceDay="+weekTime+'&goodsId='+goodsId,	
    dataType:"json", 
    success:function(res){
      console.log(res);
      if(res.status === 1){
        var data={
          data:res.data,
				};
				console.log(data)
        var html = template('time-script', data);
        document.getElementById('time').innerHTML = html;
      }
    },
    error:function(res){
          
    },
  })
};

// 获取服务人员
function getJobPeople(){
	if(time){
		$.ajax({
			url: baseUrl+"/hsent-xxyl-user/staffs/getBasiStaffs",
			type:"get",	
			data:"shopId="+shopId+
			'&goodsId='+goodsId+
			'&day='+weekTime+
			'&time='+time,	
			dataType:"json", 
			success:function(res){
				console.log(res);
				if(res.status === 1){
					jobPeopleArr = res.data;
					var data={
						data:jobPeopleArr,
						type:1,
						picUrl:picUrl
					};
					console.log(data)
					var html = template('staff-script', data);
					document.getElementById('staff-box').innerHTML = html;
				}
			},
			error:function(res){
						
			},
		})
	}else{
		var data={
			data:'请选择服务时间',
			type:0
		};
		var html = template('staff-script', data);
		document.getElementById('staff-box').innerHTML = html;
	}
};

// 获取服务评分及评价数量
function getDetailEvaluation(){
	$.ajax({
		url: baseUrl+"/hsent-xxyl-user/service/selectGoodsNums",
		type:"get",	
		data:"shopId="+shopId+
		'&goodId='+goodsId,	
		dataType:"json", 
		success:function(res){
			console.log(res);
			if(res.status === 1){
				tempData = res.data;
				tempData.round = Math.round(tempData.pingfen);
				tempData.cha = 5 - tempData.round;
				var data={
					data:tempData,
				};
				var html = template('rate-script', data);
				document.getElementById('rate').innerHTML = html;
			}
		},
		error:function(res){
					
		},
	})
};

// 获取用户评价
function getUserEvaluation(){
	$.ajax({
		url: baseUrl+"/hsent-xxyl-user/service/tEvaluationRecordUser",
		type:"get",	
		data:"shopId="+shopId+
		'&id='+goodsId+
		'&pageNum='+pageNum+
		'&pageSize='+pageSize+
		'&status='+status,	
		dataType:"json", 
		success:function(res){
			console.log(res);
			if(res.status === 1){
				if(res.data.list.length == 0){
					$('.viewmore').text('暂无更多评价');
				}else{
					if(evaluationPush){
						for(var j = 0 ; j < res.data.list.length ; j ++){
							evaluationArr.push(res.data.list[j]);
						}
					}else{
						evaluationArr = res.data.list;
					}
					for(var i = 0 ; i < evaluationArr.length ; i ++){
						evaluationArr[i].round = Math.round(evaluationArr[i].evaluateValue)
						evaluationArr[i].cha = 5 - evaluationArr[i].round;
					};
					console.log(evaluationArr)
					var data={
						data:evaluationArr,
						picUrl:picUrl
					};
					var html = template('usereva-box-script', data);
					document.getElementById('usereva-box').innerHTML = html;
				}
			}
		},
		error:function(res){
					
		},
	})
};
